<?php

namespace backend\helpers;

use yii;

class YandexCloudHelper
{
    public static function generateSpeech($speechText, $fileName)
    {
        $apiKey = 'AQVNxGt69tiLTyT0Pv31WtzNGx93rjqD4265NMbd';
        $folderId = "b1ggccses7ud0akqlu7l"; # Идентификатор каталога
        $url = "https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize";

        $post = "ssml=" . urlencode($speechText) . "&lang=ru-RU&voice=filipp";
        $headers = ['Authorization: Api-Key ' . $apiKey];
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        if ($post !== false) 
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


        $response = curl_exec($ch);

        if (curl_errno($ch)) 
        {
            print "Error: " . curl_error($ch);
        }
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) 
        {
            $decodedResponse = json_decode($response, true);
            echo "Error code: " . $decodedResponse["error_code"] . "\r\n";
            echo "Error message: " . $decodedResponse["error_message"] . "\r\n";
        } 
        else 
        {
            file_put_contents(Yii::getAlias('@frontend/web/') . 'audio-archive/' . $fileName . ".ogg", $response);
        }

        curl_close($ch);
    }

}